<?php

class Service_JD extends Service implements IService
{
    
    static $moduleName = 'Junodownload Parser';
    static $author = 'KnKill';
    static $version = '0.30';
    static $site = 'www.junodownload.com';
    static $mask = '.*junodownload.com\/products.*';
    static $metaFields = array(
        META_LABEL,
        META_GENRES,
        META_RELEASE_DATE,
        META_CATALOG,
        META_REVIEW
    );
    
    private $content;

    public function __construct($url) {
        $this->content = getPage($url);
        phpQuery::newDocumentHTML($this->content);
    }
    
    
    /**
     * getReleaseArtists
     */
    public function getReleaseArtists() {
        $_artists = array();
        $artists = pq('#product_heading_artist a');
        foreach($artists as $artist) {
            $artist = trim(pq($artist)->eq(0)->text());
            $artist = str_ireplace("&amp;", "&", $artist);
            $artist = str_ireplace("Various", "Various Artists", $artist);
            $artist = ucwords(strtolower($artist));
            $_artists[] = $artist;
        }
        return $_artists;
    }
    
    
    /**
     * getReleaseTtitle
     */
    public function getReleaseTitle() {
        $title = pq('#product_heading_title a')->eq(0)->text();
        // remove title descriptions
        $title = str_replace(array('(FORTHCOMING)', '(EXCLUSIVE)', '(FORTHCOMING EXCLUSIVE)'), '', $title);
        return trim($title);
    }
    
    
    /**
     * getGenres
     */
    public function getGenres() {
        $genre = trim(pq('#product_info_genre a')->eq(0)->text());
        $genre = str_replace(array('Drum And Bass'), array('Drum & Bass'), $genre);
        return array($genre);
    }
    
    
    /**
     * getLabel
     */
    public function getLabel() {
        $label = trim(pq('#product_heading_label a')->eq(0)->text());
        return trim($label);
    }
    
    
    /**
     * getCatalog
     */
    public function getCatalog() {
        $catalog = pq('#product_info_cat_no')->eq(0)->text();
        return trim(str_replace(" ", "", $catalog));
    }
    
    
    /**
     * getReleaseDate
     */
    public function getReleaseDate() {
        $rd = pq('#product_info_released_on')->eq(0)->text();
        return date('Y-m-d', strtotime(trim($rd)));
    }

    
    /**
     * getTracklist
     */
    public function getTracklist() {
        $tracklist = array();
        foreach(pq('.product_tracklist_records') as $element) {
            $element = pq($element);
            $index = $element->find('.product_tracklist_heading_records_sn')->eq(0)->text();
            $title = $element->find('.product_tracklist_heading_records_title')->eq(0)->text();
            $length = $element->find('.product_tracklist_heading_records_length')->eq(0)->text();
            
            if(preg_match('/(.*?) - (.*)/', trim($title), $trackData) > 0) {
                $artists = array(trim($trackData[1]));
                $title = trim($trackData[2]);
            } else {
                $artists = $this->getReleaseArtists();
            }
            
            if(empty($number) && empty($title)) break;
            $tracklist[(int)$index] = array(
                'artists' => $artists,
                'title'   => trim(ucwords(strtolower($title))),
                'length'  => trim($length)
            );
        }
        return $tracklist;
    }
    
    
    /**
     * getReview
     */
    public function getReview() {
        $review = trim(pq('#product_release_note')->eq(0)->text());
        $review = trim(str_replace('Review:', '', $review));
        return (empty($review)) ? false : $review;
    }

}